package com.yy.ttmodule.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class DataQos {
    private static final String TAG = "DataQosSettings";

    private static final String FILE_PATH = "/data/etc/psqos.conf";

    public static boolean writeData(String data) {
        Log.v(TAG, "writeData data = " + data);

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        File target = new File(FILE_PATH);
        boolean ret = false;
        try {
            target.createNewFile();
            fos = new FileOutputStream(target);
            osw = new OutputStreamWriter(fos);
            osw.write(data + " ");
            osw.flush();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.v(TAG, "writeData Exception = " + e.getMessage());
            //Toast.makeText(mContext, R.string.str_prompt_write_file_unsuccessfully, Toast.LENGTH_SHORT).show();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.v(TAG, "writeData FileOutputStream close Exception = " + e.getMessage());
                }
            }

            if (osw != null) {
                try {
                    osw.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.v(TAG, "writeData OutputStreamWriter close Exception = " + e.getMessage());
                }
            }
        }

        if (ret) {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
                Log.v(TAG, "writeData sleep Exception = " + e.getMessage());
            }
        }
        return ret;
    }

    public static String readFileData(Context context) {
        Log.v(TAG, "ReadFileData");
        FileInputStream fis = null;
        InputStreamReader isr = null;
        String data = "1 4 64 64";
        File target = new File(FILE_PATH);

        try {
            fis = new FileInputStream(target);
            isr = new InputStreamReader(fis);
            char [] buffer = new char[255];
            isr.read(buffer);
            data = new String(buffer);
            Log.v(TAG, "ReadFileData buffer[" + buffer + "]");
        } catch (Exception e) {
            e.printStackTrace();
            Log.v(TAG, "ReadFileData Exception = " + e.getMessage());
//            Toast.makeText(context, getString(R.string.str_prompt_read_file_unsuccessfully), Toast.LENGTH_SHORT).show();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.v(TAG, "ReadFileData FileInputStream close Exception = " + e.getMessage());
                }
            }

            if (isr != null) {
                try {
                    isr.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.v(TAG, "ReadFileData InputStreamReader close Exception = " + e.getMessage());
                }
            }
        }
        Log.v(TAG, "ReadFileData data = " + data);

        return data;
    }

}
